

using System;
using System.Xml;

namespace Nutils
{
    public class Exception : System.Exception
    {
        public Exception(string sMsg)
            : base(sMsg)
        {
        }
    }


    public static class Hack
    {
        public static void CheckFileLock(string fileName)
        {
            Nutils.Console.Spam("CheckFileLock: " + fileName);
            while (true)
            {
                try
                {
                    _CheckFileLock(fileName);
                    return;
                }
                catch (System.IO.IOException)
                {
                    System.Threading.Thread.Sleep(1);
                    Nutils.Console.Spam("Failed CheckFileLock: " + fileName);
                }
            }
        }

        private static void _CheckFileLock(string fileName)
        {
            if (!System.IO.File.Exists(fileName))
            {
                return;
            }

            System.IO.StreamWriter txtw = new System.IO.StreamWriter(fileName,true);
            txtw.Close();
        }
    }

    public static class Crypto
    {
        //const string _Hash = @"1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()-_=+[]{}|:;<>,.? ";
        const string _Hash = @"l26bKz&A)7fn*v=|mCiI-QJ!:LXBSNc?Doq[E%PW+Z <@{(tRMwg4TVG$j;1dHk]#_a0^xre9}Us8pF5h>,.uyOY3";
        public static string Encrypt(string val, int key)
        {
            int n = 0;
            string sResult = "";
            foreach (char c in val)
            {
                sResult += _Hash[(_Hash.IndexOf(c) + key + n) % _Hash.Length];
                ++n;
            }

            return sResult;
        }
        public static string Decrypt(string val, int key)
        {
            int n = 0;
            string sResult = "";
            foreach (char c in val)
            {
                int i = (_Hash.IndexOf(c) - key - n);
                if (i < 0)
                {
                    i += _Hash.Length;
                }
                sResult += _Hash[i];
                ++n;
            }

            return sResult;
        }

    }
}
